<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <!-- <img alt="logo" class="logo" src="@/assets/img/logo.png" /> -->
        <span class="title">{{ systemName }}1</span>
      </div>
      <div class="desc">博特智能</div>
    </div>
    <div class="login">
      <a-form-model ref="loginForm" :model="form" :rules="rules">
        <!-- <a-tabs
          size="large"
          :tabBarStyle="{ textAlign: 'center' }"
          style="padding: 0 2px"
        > -->
        <!-- <a-tab-pane tab="用户名登录" key="1"> -->
        <a-alert
          type="error"
          :closable="true"
          v-show="error"
          :message="error"
          showIcon
          style="margin-bottom: 24px"
        />
        <a-form-model-item prop="username">
          <a-input
            autocomplete="autocomplete"
            size="large"
            placeholder="请输入用户名"
            v-model="form.username"
          >
            <a-icon slot="prefix" type="user" />
          </a-input>
        </a-form-model-item>
        <a-form-model-item prop="password">
          <a-input-password
            size="large"
            placeholder="请输入密码"
            autocomplete="autocomplete"
            v-model="form.password"
          >
            <a-icon slot="prefix" type="lock" />
          </a-input-password>
        </a-form-model-item>
        <a-form-model-item prop="vercode">
          <a-input
            style="width:58%;margin-right:6px"
            size="large"
            placeholder="请输入验证码"
            autocomplete="autocomplete"
            v-model="form.vercode"
          >
          </a-input>
          <img
            :src="codeImg"
            height="42"
            class="position-r cursorPoint"
            style="top:-1px"
            @click="getImgCode"
          />
        </a-form-model-item>
        <a-form-model-item prop="vercode" v-show="false">
          <a-input
            size="large"
            autocomplete="autocomplete"
            v-model="form.vercodeToken"
          >
          </a-input>
        </a-form-model-item>
        <!-- </a-tab-pane> -->
        <!-- <a-tab-pane tab="忘记密码" key="2">
            <a-form-item>
              <a-input size="large" placeholder="请输入手机号" >
                <a-icon slot="prefix" type="mobile" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-row :gutter="8" style="margin: 0 -4px">
                <a-col :span="16">
                  <a-input size="large" placeholder="请输入验证码">
                    <a-icon slot="prefix" type="mail" />
                  </a-input>
                </a-col>
                <a-col :span="8" style="padding-left: 4px">
                  <a-button style="width: 100%" class="captcha-button" size="large">获取验证码</a-button>
                </a-col>
              </a-row>
            </a-form-item>
          </a-tab-pane> -->
        <!-- </a-tabs> -->

        <a-form-item>
          <a-button
            :loading="logging"
            style="width: 100%; margin-top: 24px"
            size="large"
            @click="onSubmit('loginForm')"
            htmlType="submit"
            type="primary"
            >登录</a-button
          >
        </a-form-item>
      </a-form-model>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from "@/layouts/CommonLayout";
import { login, getRoutesConfig } from "@/services/user";
import { code } from "@/services/system";
import { setAuthorization } from "@/utils/request";
import { loadRoutes } from "@/utils/routerUtil";
import { mapMutations } from "vuex";
// import { rule } from "@/utils/validateRules";
import { timeFix, sortMenu, sortPermission } from "@/utils/common";

export default {
  name: "Login",
  components: { CommonLayout },
  data() {
    return {
      logging: false,
      error: "",
      form: {
        username: "",
        password: "",
        vercode: "",
        vercodeToken: ""
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      },
      codeImg: ""
    };
  },
  created() {
    this.form.username = sessionStorage.getItem("loginName") || "";
    this.getImgCode();
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    }
  },
  methods: {
    ...mapMutations("account", [
      "setUser",
      "setPermissions",
      "setRoles",
      "setIconMap"
    ]),
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.logging = true;
          sessionStorage.setItem("v3.loginName", this.form.username);
          login(
            this.form.username,
            this.form.password,
            this.form.vercode,
            this.form.vercodeToken
          ).then(this.afterLogin);
        } else {
          return false;
        }
      });
    },
    getImgCode() {
      code().then(res => {
        if (res.data.code == 0) {
          this.codeImg = res.data.data.imageBase64Data;
          this.form.vercodeToken = res.data.data.vercodeToken;
        }
      });
    },
    afterLogin(res) {
      this.logging = false;
      const loginRes = res.data;
      if (loginRes.code == 0) {
        setAuthorization({ token: loginRes.data.accessToken });
        const userInfo = loginRes.data.userInfo;
        //存入用户信息
        this.setUser(userInfo);
        if (!userInfo.pwdStatus) {
          this.$router.push("resetPwd");
          return;
        }
        // 获取路由配置
        getRoutesConfig().then(res => {
          this.setIconMap(res.data.data);
          //整理菜单和权限数据
          let routesConfig = sortMenu(res.data.data);
          let permissionConfig = sortPermission(res.data.data);
          this.setPermissions(permissionConfig);
          loadRoutes(routesConfig);
          console.log(permissionConfig);
          let firstPage = routesConfig[0].children[0].children[0]
            ? `${routesConfig[0].children[0].router}/${routesConfig[0].children[0].children[0]}`
            : routesConfig[0].children[0].router;
          console.log(firstPage);
          this.$router.push(firstPage);
          this.$message.success(timeFix() + "，欢迎回来", 3);
        });
      } else {
        this.error = loginRes.message;
        this.form.vercode = ''
        this.getImgCode();
      }
    }
  }
};
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: @title-color;
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 50px auto 0 auto;
    .captcha-button {
      color: #fff;
    }
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
    /deep/ .ant-input {
      height: 42px;
      font-size: 14px;
    }
    .ant-btn {
      background-color: #2979e7;
      border-radius: 0;
      &:hover {
        background-color: #5494ec !important;
        border-color: #5494ec !important;
      }
    }
  }
}
</style>
